/**
 *js公共文件 
 */

//URL 请求类
var urlRequest = {
	callback:null,
	url:'',
	
	//url编码
	urlencode: function (str)
	{
		return encodeURIComponent(str);
	},

	//url解码
	urldecode: function (str)
	{
		return decodeURIComponent(str);
	},

	//取得url中参数值
	getParameter: function (varName,query){
		if (typeof query == "undefined") query = window.location.search;
		if (query != null || query != ""){
			query = query.replace(/^\?+/, "");
			var qArray1 = qArray = '';
			if(query.indexOf('?')> -1){
			    qArray1 = query.split("?");
                qArray = qArray1[1].toString().split("&");

			}else{
			    qArray = query.split("&");
			}
			var len = qArray.length;
			if (len > 0){
				for (var i=0; i<len; i++){
					var sArray = qArray[i].split("=", 2);
					if (sArray[0] && sArray[1] && sArray[0] == varName)
					{
						return unescape(sArray[1]);
					}
				}
			}
		}
		return null;
	},
	//设置URL参数
	setParameter:function(name,value,url){
		if (typeof url == "undefined"){
			url = window.location.href;
		}
		hash = '';
		if(url.indexOf('#')>-1){
			hash = '#'+url.substr(url.indexOf('#')+1);
			url = url.substr(0,url.indexOf('#'));
		}

		oldvalue = this.getParameter(name,url);
		if(oldvalue==null){
			str = name+"="+this.urlencode(value); 
			if(url.indexOf('?')>-1){
				url = url+"&"+str;
			}else{
				url = url+"?"+str;
			}
		}else{
 			parttern = name+'='+oldvalue;
			url = url.replace(parttern,name+'='+this.urlencode(value));
 		}
 		return url+hash;
	},
	//删除参数
	unsetParameter:function(name,url){
		if (typeof url == "undefined") url = window.location.href;
		var regexp = name+"=([^&]*)";
		url = itbeing.replace(url,regexp,'');
		url = itbeing.replace(url,'&&','&');
		return url;
	},
	//排序
	setSort:function(field){
   		url = this.setParameter('order',field);
		sort = this.getParameter('sort');
 		if(sort=='asc') sort = 'desc';
		else sort = 'asc';
		this.url = this.setParameter('sort',sort,url);
		if(this.callback!=null){
			this.callback();
		}else{
			if(this.url!=window.location.href) window.location.href = this.url;
		}	
	},
	//分页
	setPagesize:function(ps){
		url = lib.setParameter( 'page',0, lib.setParameter('pagesize',ps) );
		if(url!=window.location.href)window.location.href = url;
	}
};